﻿/**
  @file
  @author Stefan Frings
*/

#include "httprequesthandler.h"
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>

using namespace stefanfrings;

HttpRequestHandler::HttpRequestHandler(QObject* parent)
    : QObject(parent)
{}

HttpRequestHandler::~HttpRequestHandler()
{}

void HttpRequestHandler::service(HttpRequest& request, HttpResponse& response)
{
    qCritical("HttpRequestHandler: you need to override the service() function");
    qDebug("HttpRequestHandler: request=%s %s %s",request.getMethod().data(),request.getPath().data(),request.getVersion().data());
    response.setStatus(501,"not implemented");
    response.write("501 not implemented",true);
}

QString HttpRequestHandler::getRequestPath(HttpRequest &request, int level)
{
    QString path = request.getPath();
    QStringList pathList = path.split("/");
    if(level == 0){
        return path;
    }else{
        if (pathList.size() < level)
        {
            return "";
        }else{
            return pathList.value(level);
        }
    }
}

void HttpRequestHandler::returnError(HttpResponse &response, int errorType)
{
    switch (errorType) {
    case 0:
        response.setStatus(404,"not found");
        response.write("404 not found",true);
        break;
    case 1:
        response.setStatus(500, "服务端异常");
        response.write("数据缓存异常",true);
        break;
    }
}
